﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRIS.Models;

namespace IRIS.Controllers
{
    public class AboutUsController : Controller
    {
        //
        DataRepo m_Db = new DataRepo();
        // GET: /AboutUs/

        
        //Listing of all the staff
        public ActionResult Staff()
        {
            var data = m_Db.getStaff();
            return View(data);
        }

        // Create Staff 
        [HttpGet]
        public ActionResult Create()
        {
            Staff sData = new Staff();

            return View(sData);
        }


        [HttpPost]
        public ActionResult Create(Staff sData)
        {
            if (string.IsNullOrEmpty(sData.Name))
                ModelState.AddModelError("Name", "Þessi reitur verður að vera útfylltur");

            sData.Alive = true;

            try
            {
                if (ModelState.IsValid)
                {
                    //everything is alright and we commit to database
                    m_Db.addStaff(sData);
                    return RedirectToAction("Staff");
                }
                else
                {
                    //something is wrong and we return the model back with error state
                    return View(sData);
                }
            }
            catch
            {
                //Something is not o.k so we return error state
                return View(sData);
            }

        }

        //Edit staff
        [HttpGet]
        public ActionResult Edit(int? id, Boolean delete)
        {
            if (id.HasValue)
            {
                int realId = id.Value;
                Staff nData = m_Db.getStaffById(realId);
                if (nData != null && delete == true)
                {
                    nData.Alive = false;
                    m_Db.editStaff(id.Value, nData);
                    return RedirectToAction("Staff");
                }
                else if (nData != null && delete == false)
                {
                    return View(nData);
                }
            }
            return View("NotFound");
        }

        
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int id,Staff theData)
        {
            m_Db.editStaff(id,theData);
            return RedirectToAction("Staff");
        }

        // This function will only get the Static page with the id of 6
        [HttpGet]
        public ActionResult UmOkkur()
        {
            var data = m_Db.getStaticById(6);
            return View(data);
        
               
        }
       
    }
}
